package com.dtsheep.rxjava.stduy.observable;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.schedulers.Schedulers;

/**
 * MapDemo
 *
 * <p>
 *     map操作符
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/17
 */
public class MapDemo {

    /**
     * map 处理元素
     *
     * @param
     * @return
     * @author chuanbai
     */
    public void map1(){
        // 创建 被观察对象 、 发射器 、 调度器
        Observable<String> observable = Observable.unsafeCreate((Subscriber<? super String> emmiter) -> {
            emmiter.onNext("11111");
            emmiter.onNext("test");
            emmiter.onNext("小猫叫");
            emmiter.onCompleted();
        }).subscribeOn(Schedulers.computation());

        // map 处理元素
        Observable<String> obsMap = observable.map(obj -> obj);
        Observable<Integer> observable1 = obsMap.map(String::length);
        Subscription subscription = obsMap.subscribe( str -> {
            System.out.println(str);
        });
        Subscription subscription1 = observable1.subscribe(len -> {
            System.out.println(len);
        });

        // 阻塞获取第一个元素
        String first = obsMap.toBlocking().first();
        System.out.println(first);


    }

    public static void main(String[] args) {
        MapDemo mapDemo = new MapDemo();
        mapDemo.map1();
    }
}
